
***********CALCULATE DJIA CONVEXITY*************
qui{
foreach j in 20 40 60 80 {
import delimited "$jfqa_rep/DJIA.csv", clear

destring ret, force replace
sort year month day

rename ret dj_ret
gen dj_change=djia-djia[_n-1]

gen prc_0=djia
qui{
forval i=1/`j' {
	gen prc_`i'=djia[_n-`i']
}


gen ret_0=dj_ret
forval i=1/81 {
	gen ret_`i'=dj_ret[_n-`i']
}

forval i=1/21 {
	gen futret_`i'=dj_ret[_n+`i']
}
}

drop if missing(prc_`j')

egen prc_avg=rmean(prc_*)

gen convexity=((prc_0+prc_`j')/2-prc_avg)/((prc_0+prc_`j')/2)
gen lag_convexity=convexity[_n-1]

drop prc* djia dj_ret

save "$jfqa_rep/21_DJIA_convexity_`j'.dta", replace
}
}
